package edu.umd.clip.lm.factors;

import org.tartarus.snowball.*;
import org.tartarus.snowball.ext.*;

public class SnowballEnglishSuffix implements WordToOvertFactorInterface {

	public SnowballEnglishSuffix() {}
	
	public String wordToOvertFactor(String word) {
		word = word.toLowerCase();
		
		SnowballStemmer stemmer = new englishStemmer();
		stemmer.setCurrent(word);
		stemmer.stem();
		String root = stemmer.getCurrent();
		
		String suffix = "";
		if (root.length() == word.length() || root.length() == 0/* && root.charAt(root.length()-1) == 'i' && word.charAt(word.length()-1) == 'y'*/) {
			//suffix = "";
		} else if (word.startsWith(root)) {
			suffix = word.substring(root.length());
			if (suffix.charAt(0) == root.charAt(root.length()-1) && 
					"zxcvbnmsdfghjklqrtp".contains(suffix.subSequence(0, 1)))
			{
				// duplicated consonant
				suffix = suffix.substring(1, suffix.length());
			} else if (root.charAt(root.length()-1) == 'e' && suffix.equals("d")) {
				suffix = "ed";
			}
		} else if (root.charAt(root.length()-1) == 'e') {
			root = root.substring(0, root.length()-1);
			if (word.startsWith(root)) {
				suffix = word.substring(root.length());
			}
		} else if (root.charAt(root.length()-1) == 'i' && word.charAt(root.length()-1) == 'y') {
			root = root.substring(0, root.length()-1) + 'y';
			if (word.startsWith(root)) {
				suffix = word.substring(root.length());
			}
		}
		return suffix;
	}

}
